package com.aidex.web.controller.biz;

import com.aidex.biz.domain.O2oOrder;
import com.aidex.biz.service.O2oOrderService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 团购预约Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/biz/bizO2oOrder")
public class O2oOrderController extends BaseController
{
    @Autowired
    private O2oOrderService o2oOrderService;

    /**
     * 查询团购预约列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:list')")
    @GetMapping("/list")
    public R<PageInfo> list(O2oOrder o2oOrder, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            o2oOrder.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        o2oOrder.setPage(new PageDomain(request, response));
        return R.data(o2oOrderService.findPage(o2oOrder));
    }

    /**
     * 获取团购预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:query')")
    @GetMapping(value = "/{id}")
    public R<O2oOrder> detail(@PathVariable("id") String id)
    {
        return R.data(o2oOrderService.get(id));
    }

    /**
     * 新增团购预约
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:add')")
    @Log(title = "团购预约", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  O2oOrder o2oOrder)
    {
        return R.status(o2oOrderService.save(o2oOrder));
    }

    /**
     * 修改团购预约
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:edit')")
    @Log(title = "团购预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated O2oOrder o2oOrder)
    {
        return R.status(o2oOrderService.save(o2oOrder));
    }


    /**
     * 删除团购预约
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:remove')")
    @Log(title = "团购预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(o2oOrderService.deleteO2oOrderByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(o2oOrderService.findMaxSort(new O2oOrder()));
    }

    /**
     * 导出团购预约列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizO2oOrder:export')")
    @Log(title = "团购预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(O2oOrder o2oOrder)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    o2oOrder.setCompanyId(null);
            }else{
                         o2oOrder.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<O2oOrder> list = o2oOrderService.findList(o2oOrder);
        ExcelUtil<O2oOrder> util = new ExcelUtil<O2oOrder>(O2oOrder.class);
        return util.exportExcel(list, "团购预约数据");
    }

}
